<template>
  <div>
    <Card id="userinfo"
          style="width:230px">
      <div class="info"
           style="text-align:center">
        <div v-if="hasGetInfo"
             class="haslogin toppart">
          <div class="avatar">
            <img :src="avatorImgPath">
          </div>
          <ul>
            <li>
              <Tooltip content="你有3条未读消息"
                       placement="bottom-end">
                <Badge dot>
                  <Icon @click="goInformation"
                        type="ios-notifications-outline"
                        size="26"></Icon>
                </Badge>
              </Tooltip>
            </li>
            <li class="name">{{name}}</li>
            <li>{{roleName}}</li>
            <li>
              <Button type="text"
                      @click="logout">退出</Button>
            </li>
          </ul>
        </div>
        <div v-if="!hasGetInfo"
             class="nologin toppart">
          <!-- <Form ref="forms"
                :model="forms"
                :rules="rules"
                inline>
            <FormItem prop="userName">
              <Input type="text"
                     v-model="forms.userName"
                     placeholder="用户名">
              <Icon type="ios-person-outline"
                    slot="prepend"></Icon>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input type="password"
                     v-model="forms.password"
                     placeholder="密码">
              <Icon type="ios-lock-outline"
                    slot="prepend"></Icon>
              </Input>
            </FormItem>
          </Form> -->
          <div class="avatar">
            <img src="../../assets/img/avatar.png">
          </div>
          <ul>
            <li></li>
            <li class="name">游客</li>
            <li>欢迎游客</li>
            <li></li>
          </ul>
        </div>
      </div>
      <Divider />
      <div class="btn">
        <div v-if="!hasGetInfo">
          <!-- <div>
            <Button type="primary"
                    @click="submit">登录</Button>
          </div>
          <div>
            <Button type="primary"
                    @click="register">注册</Button>
          </div> -->

          <div>
            <Button type="primary"
                    @click="goBack">返回门户</Button>
          </div>
        </div>
        <div v-if="hasGetInfo">
          <div v-if="roleId=='4'">欢迎您对我们进行指导评价！</div>
          <div>
            <Button type="primary"
                    @click="goPersonal">
              <Icon type="md-person"
                    size="24" />个人中心</Button>
          </div>
          <div v-if="roleId=='3'">
            <Button type="primary"
                    @click="goStudy">
              <Icon type="ios-construct"
                    size="24" />学习辅助</Button>
          </div>
          <div v-if="roleId=='2'">
            <Button type="primary"
                    @click="goStudy">
              <Icon type="ios-construct"
                    size="24" />教学辅助</Button>
          </div>
          <div v-if="roleId=='1'">
            <Button type="primary"
                    @click="goManagement">
              <Icon type="md-settings"
                    size="24" />后台管理</Button>
          </div>
        </div>
      </div>
    </Card>
    <register ref="register"></register>
  </div>
</template>
<script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
import { clearLoginInfo } from '@/libs'
import { logout } from '@/api/user'
import register from '../bizs/management/user-management-add-update'
export default {
  data () {
    return {
      loading: false,
      forms: {
        userName: '',
        password: ''
      },
      rules: {
        userName: {
          required: true,
          message: '用户名不能为空',
          trigger: 'blur'
        },
        password: {
          required: true,
          message: '密码不能为空',
          trigger: 'blur'
        }
      }
    }
  },
  computed: {
    ...mapState({
      hasGetInfo: state => state.user.hasGetInfo,
      userName: state => state.user.userName,
      name: state => state.user.name,
      userId: state => state.user.userId,
      avatorImgPath: state => state.user.avatorImgPath,
      roleName: state => state.user.roleName,
      roleId: state => state.user.roleId,
      messageUnreadList: state => state.user.messageUnreadList,
      messageReadedList: state => state.user.messageReadedList,
      messageTrashList: state => state.user.messageTrashList,
      messageList () {
        return this[listDic[this.currentMessageType]]
      },
      titleClass () {
        return {
          'not-unread-list': this.currentMessageType !== 'unread'
        }
      }
    }),
    ...mapGetters([
      'messageUnreadCount',
      'messageReadedCount',
      'messageTrashCount'
    ])
  },
  methods: {
    ...mapActions(['handleLogin', 'getUserInfo']),
    //消息中心
    goInformation () {
      this.$router.push({ name: 'information' })
    },
    // 个人中心
    goPersonal () {
      this.$router.push({ name: 'personal' })
    },
    // 学习辅助
    goStudy () {
      this.$router.push({ name: 'study' })
    },
    // 用户管理
    goManagement () {
      this.$router.push({ name: 'user-management' })
    },
    // 登录
    async submit () {
      let result = await this.$refs.forms.validate()
      if (!result) return
      // const user = DBUser.findUser(this.forms)
      // if (user) {
      //   this.$message({
      //     message: '登录成功',
      //     type: 'success'
      //   })
      //   this.$store.dispatch('user/Login', user)
      //   this.$router.push({ name: 'Todo' })
      // } else {
      //   this.$message({
      //     message: '账号或密码不正确！',
      //     type: 'warning'
      //   })
      // }
      let { userName, password } = this.forms
      this.handleLogin({ userName, password }).then(res => {
        this.getUserInfo(userName).then(res => {
          this.$router.replace({ name: 'home' })
        })
      })
    },
    // 注册
    register () {
      this.$refs.register.init()
    },
    // 退出登录
    logout () {
      this.$router.push({
        name: 'login'
      })
      clearLoginInfo()
    },
    goBack () {
      this.$router.push({
        name: 'login'
      })
    }
  },
  components: {
    register
  }
}
</script>
<style lang="scss">
#userinfo {
  width: 230px;
  height: 280px;
  overflow: hidden;
  box-shadow: 0 4px 8px 0 rgba(7,17,27,.1);
  .info {
    width: 100%;
    height: 100%;
    .toppart {
      display: flex;
      padding: 5px 0;
      height: 50%;
      .avatar {
        position: relative;
        width: 82px;
        flex: 0 0 82px;
        height: 0;
        padding-top: 82px;
        border-radius: 100%;
        overflow: hidden;
        & > img {
          position: absolute;
          left: 0;
          top: 0;
          width: 82px;
          height: 82px;
        }
      }
      ul {
        width: 100%;
        padding-left: 10px;
        li {
          width: 100%;
          text-align: left;
          height: 24px;
          line-height: 24px;
          &:first-of-type {
            text-align: right;
          }
          &:last-of-type {
            text-align: right;
          }
          .ivu-btn {
            font-size: 16px;
            padding: 0;
          }
          .ivu-icon {
            cursor: pointer;
          }
        }
        .name {
          font-size: 16px;
          font-weight: bolder;
        }
      }
    }
  }
  .ivu-divider-horizontal {
    margin: 0;
  }
  .btn {
    font-size: 16px;
    div {
      .ivu-btn {
        width: 100%;
        height: 50px;
        .ivu-icon {
          margin-right: 5px;
        }
      }
      margin-top: 10px;
      &:first-of-type {
        margin-top: 25px;
        font-size: 14px;
      }
    }
  }
}
</style>



